﻿using System;
using System.Collections.Generic;
using System.Text;
using Common.Exceptions;

namespace Common.Cache
{
    /// <summary>
    /// redis缓存接口
    /// </summary>
    public interface IRedisCache : ICache
    {
        /// <summary>
        /// 批量设置键值对缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyValues"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        bool MSet<T>(Dictionary<string, T> keyValues, TimeSpan? expiry);
        /// <summary>
        /// HashGet
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        string HashGet(string key, string field);
        /// <summary>
        /// HashGet强类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        T HashGet<T>(string key, string field);
        /// <summary>
        /// 设置hash缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        bool HashSet(string key, string field, object value, TimeSpan? expiry = null);
        /// <summary>
        /// 设置hash缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="keyValues"></param>
        /// <returns></returns>
        bool HashSet<T>(string key, IDictionary<string, T> keyValues);
        /// <summary>
        /// 获取redis分布式锁，此方法将会阻塞线程，直到获取锁或者超时，超时将会抛出异常，请自行捕捉
        /// </summary>
        /// <param name="lockName">锁名称</param>
        /// <param name="timeout">超时时间，单位秒，此时间内未获得锁将抛出异常</param>
        /// <param name="autoDelay">是否自动续期，当超时时间已过，业务仍然未释放锁时对锁续期</param>
        /// <exception cref="LockTimeoutException"></exception>
        /// <returns></returns>
        IDistributedLock Lock(string lockName, int timeout, bool autoDelay = true);
        /// <summary>
        /// 执行redis脚本
        /// </summary>
        /// <param name="script"></param>
        /// <param name="keys"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        object Eval(string script, string[] keys = null, params string[] parameters);
        /// <summary>
        /// 设置key的过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        bool KeyExpire(string key, TimeSpan expiry);
        /// <summary>
        /// 切换redis数据库
        /// </summary>
        /// <param name="dbIndex"></param>
        /// <returns></returns>
        IRedisCache Switch(int dbIndex);
    }
}
